import java.awt.*;
import java.util.ArrayList;

public class Planet
	{
		private Color										planetColor;
		private static final int				side	= 60;
		private final Point							position;
		private int											antalPlan;
		private int											drawtedSide;
		private final ArrayList<Point>	pointsOnFrame;
		private boolean									selected;
		
		public Planet(int antalPlan, Color color, int x, int y)
			{
				this.antalPlan = antalPlan;   //antalet skepp pa� planeten
				drawtedSide = 10 + (this.antalPlan);
				this.planetColor = color;   //om vem som planeten tillha�r till
				this.position = new Point(x, y);
				pointsOnFrame = new ArrayList<Point>(); //alla punkter pa� planetet
				int radius = side / 2;
				x = x - radius;
				y = y - radius;
				//int a = 0;
				for(int i=0;i<side; i++)
					{
						pointsOnFrame.add(new Point(x+i, y));
						pointsOnFrame.add(new Point(x, y+i));
						pointsOnFrame.add(new Point(x+i, y+side));
						pointsOnFrame.add(new Point(x+side, y+i));
					}
				selected = false; // om spelaren har klickat på denna planet
			}
		
		public Planet()
			{
				this(0, null, 0, 0);
			}
		
		public static boolean krock(Planet p1, Planet p2)
			{
				boolean b = false;
				for (int i = 0; i < p1.pointsOnFrame.size(); i++)
					{
						for (int j = 0; j < p2.pointsOnFrame.size(); j++)
							{
								//								System.out.println("i: "+i + 
								//										" j: " +j + 
								//										" p2.l: " +p2.punkter.length + 
								//										" Ponit: " + p2.punkter[j]);
								if (p1.pointsOnFrame.get(i).x == p2.pointsOnFrame.get(j).x && p1.pointsOnFrame.get(i).y == p2.pointsOnFrame.get(j).y)
									{
										b = true;
									}
								
							}
					}
				
				return b;
			}
		
		public static Planet nyNetralPlanet(int width, int height)
			{
				return new Planet((int) (Math.random() * 49 + 1), Color.GRAY, (int) (Math.random() * (width * 0.8) + 1), (int) (Math.random()
						* (height * 0.8) + 1));
			}
		
		public static Planet nySpelPlanet(Color c, int width, int height)
			{
				return new Planet(50, c, (int) (Math.random() * (width * 0.8) + 1), (int) (Math.random() * (height * 0.8) + 1));
			}
		
		public void ritaUt(Graphics g)
			{
				g.setColor(planetColor);
				g.fillOval(position.x - drawtedSide / 2, position.y - drawtedSide / 2, drawtedSide, drawtedSide);
				if (this.planetColor.equals(Color.RED))
					{
						g.setColor(Color.BLUE);
					}
				else if (this.planetColor.equals(Color.BLUE))
					{
						g.setColor(Color.RED);
					}
				else
					{
						g.setColor(Color.BLACK);
					}
				g.drawString("" + antalPlan, (int) (position.x - drawtedSide * 0.1), (int) (position.y + drawtedSide * 0.1));
			}
		
		//returnerar id av den klickade planeten
		public int getSelectedPlanetID(Point p, ArrayList<Planet> planets)
			{
				int x = (int) p.getX();
				int y = (int) p.getY();
				for (int i = 0; i < planets.size(); i++)
					{
						for (int j = 0; j < planets.get(i).pointsOnFrame.size(); j++)
							{
								if (planets.get(i).pointsOnFrame.get(j).x == x && planets.get(i).pointsOnFrame.get(j).y == y)
									{
										return (i);
										
									}
							}
					}
				return (404);//default planet not found
			}
		
		public int getSide()
			{
				return side;
			}
		public boolean isSelected()
			{
				return selected;
			}
		
		public void select()
			{
				this.selected = true;
			}
		
		public void unSelect()
			{
				this.selected = false;
			}
		
		/**
		 * @return the antalPlan
		 */
		public int getAntalPlan()
			{
				return antalPlan;
			}
		
		/**
		 * @param antalPlan
		 *        the antalPlan to set
		 */
		public void setAntalPlan(int antalPlan)
			{
				this.antalPlan = antalPlan;
				this.drawtedSide = 10 + (antalPlan);
			}
		
		/**
		 * @return the drawtedSide
		 */
		public int getDrawtedSide()
			{
				return drawtedSide;
			}
		
		/**
		 * @param drawtedSide
		 *        the drawtedSide to set
		 */
		public void updateDrawtedSide()
			{
				this.drawtedSide = 10 + (antalPlan);
			}
		/**
		 * @return the position
		 */
		public Point getPosition()
			{
				return position;
			}
		
		/**
		 * @param drawtedSide
		 *        the drawtedSide to set
		 */
		public void setDrawtedSide(int drawtedSide)
			{
				this.drawtedSide = drawtedSide;
			}
		
		/**
		 * @param selected
		 *        the selected to set
		 */
		public void setSelected(boolean selected)
			{
				this.selected = selected;
			}

		/**
		 * @return the planetColor
		 */
		public Color getPlanetColor()
			{
				return planetColor;
			}

		/**
		 * @param planetColor the planetColor to set
		 */
		public void setPlanetColor(Color planetColor)
			{
				this.planetColor = planetColor;
			}

		/**
		 * @return the pointsOnFrame
		 */
		public ArrayList<Point> getPointsOnFrame()
			{
				return pointsOnFrame;
			}
	}
